package algorithm.weightgraph;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class SparseWeightGraph<Weight extends Number&Comparable> extends AbstractGraph<Weight> {


    private List<Edge<Weight>>[] edges;


    public SparseWeightGraph(int pointNumns, boolean directed){
        this.pointNums = pointNumns;
        this.directed = directed;
        this.edges = new LinkedList[pointNumns];
        for (int i = 0; i < pointNumns; i++) {
            edges[i] = new LinkedList<>();
        }
    }

    @Override
    public void addEdge(int s, int e, Weight weight) {
        edges[s].add(new Edge<>(s,e,weight));
        if (!directed){
            edges[e].add(new Edge<>(e,s,weight));
        }
        edgeNums++;
    }


    @Override
    public boolean hasEdge(int s, int e) {
        List<Edge<Weight>> list = edges[s];
        for (Edge<Weight> weightEdge : list) {
            if (weightEdge.getS()==s&&weightEdge.getE()==e){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator<Edge<Weight>> getIterator(int s) {
        return edges[s].iterator();
    }

    @Override
    public void show() {
        for (List<Edge<Weight>> list : edges) {
            for (Edge<Weight> edge : list) {

                String msg = edge.getWeight().toString();
                if (msg.length()<4){
                    for (int i = 0; i < 4-msg.length(); i++) {
                        msg+="0";
                    }
                }
                System.out.print(msg+"\t");
            }
            System.out.println("");
        }
    }
}
